package main

import (
	"github.com/kataras/iris"
	"github.com/lestrrat-go/file-rotatelogs"
	log "github.com/sirupsen/logrus"
	"src/config"
	"src/handler"
	"src/logger"
	"time"
)

func main() {
	// 初始化log
	logf := logInit()
	defer logf.Close()
	app := handler.NewApp()
	if e := app.Run(iris.Addr(":8081")); e != nil {
		panic(e.Error())
	}

}

func logInit() *rotatelogs.RotateLogs {
	configIns := config.GetConfigInstance()
	logLev := configIns.Log.Level
	RotationTime := configIns.Log.RotationTime
	if RotationTime <= 0 {
		panic("illegal RotationTime")
	}
	RotationCount := configIns.Log.RotationCount
	if RotationCount <= 1 && RotationCount > 365 {
		panic("illegal RotationCount")
	}

	// 日志
	logf, err := rotatelogs.New(
		"log/log%Y%m%d",
		rotatelogs.WithLinkName("log/log"),
		rotatelogs.WithRotationTime(time.Second*time.Duration(RotationTime)),
		rotatelogs.WithRotationCount(uint(RotationCount)),
	)
	if err != nil {
		panic(err)
	}
	log.AddHook(&logger.DefaultFieldHook{})
	log.SetOutput(logf)
	log.SetReportCaller(true)
	log.SetFormatter(&logger.DefaultFormatter{})
	level, e := log.ParseLevel(logLev)
	if e != nil {
		panic(e.Error())
	}
	log.SetLevel(level)
	return logf
}
